package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    private BaseTrademarkMapper baseTrademarkMapper;
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    public BaseCategoryTrademarkServiceImpl(BaseTrademarkMapper baseTrademarkMapper, BaseCategoryTrademarkMapper baseCategoryTrademarkMapper) {
        this.baseTrademarkMapper = baseTrademarkMapper;
        this.baseCategoryTrademarkMapper = baseCategoryTrademarkMapper;
    }

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        List<BaseTrademark> baseTrademarkList = new ArrayList<>();

        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.baseCategoryTrademarkMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> trademarkIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            LambdaQueryWrapper<BaseTrademark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(BaseTrademark::getId, trademarkIdList);
            baseTrademarkList = this.baseTrademarkMapper.selectList(lambdaQueryWrapper);
        }

        return baseTrademarkList;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        LambdaQueryWrapper<BaseCategoryTrademark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id)
                .eq(BaseCategoryTrademark::getTrademarkId, trademarkId);

        this.baseCategoryTrademarkMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> baseTrademarkListResult = new ArrayList<>();

        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.baseCategoryTrademarkMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> trademarkIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            LambdaQueryWrapper<BaseTrademark> baseTrademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            baseTrademarkLambdaQueryWrapper.notIn(BaseTrademark::getId, trademarkIdList);
            baseTrademarkListResult = this.baseTrademarkMapper.selectList(baseTrademarkLambdaQueryWrapper);
        }
        return baseTrademarkListResult;
    }

    @Override
    public void save(CategoryTrademarkVo trademarkVo) {
        List<Long> trademarkIdList = trademarkVo.getTrademarkIdList();
        List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(trademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(trademarkId);
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        this.saveBatch(baseCategoryTrademarkList);
    }

}
